<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use DB;
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

       $data = DB::table("user")->get();
       // dd($data);
       //view("分配模板","分配数据")
       return view('user.index', ['data' => $data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    //执行添加页面  {{url('user/create')}}
    public function create()
    {
       return view("user.add");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    //用户添加  路由{{url('user')}} store post提交 对象Request
    public function store(Request $request)
    {
        //insertGetId 返回的是自增ID
        //$res = DB::table('new')->insert( ['title' => $title,"aid"=>$aid]);
        //判断是否有上传
        if($request->hasFile("upfile")){
            //获取上传信息
            $file = $request->file("upfile");
            //确认上传的文件是否成功
            if($file->isValid()){
                //$picname = $file->getClientOriginalName(); //获取上传原文件名
                $ext = $file->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $filename = time().rand(1000,9999).".".$ext;
                $file->move("./upload/",$filename);
                                
                // return response($filename); //输出
                // exit();
                $time = time();
                $pwd = md5($request->password);
                // $file = "./upload/".$filename;        
                $res = DB::table('user')->insert( ['name' => $request->name,"password"=>$pwd,"lasttime"=>$time,"photo"=>$filename]);
                if($res > 0){
                    echo "添加成功";
                    return redirect('user');
                }else{
                    echo "添加失败";
                }
            }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return "浏览单个页面";
    }

    /**
     * Show the form for editing the specified resource.
     *编辑页面
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
       //
       $id = $request->route("new");
       $data = DB::table("new")->where("id",$id)->get();
       return view('new.edit', ['data' => $data]);
    }

    /**
     * Update the specified resource in storage.
     *编辑页面的提交{{url('new/updata')}}
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $id = $request->id;
        $catid = $request->catid;
        $title = $request->title;
        $res = DB::table('new')->where('id', $id)->update(['catid' => $catid,"title"=>$title]);
        if($res){
            echo "修改成功";
        }else{
            echo "修改失败";
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        return "执行删除页面";
    }

    //删除操作
    public function delete(Request $request)
    {
        $id = $request->id;
        $res = DB::table("user")->where('id',$id)->delete();
        if($res){
            return redirect('user');
        }else{
            echo "删除失败";
        }
    }

    //文件上传操作
    public function doUpload(Request $request)
    {
        //判断是否有上传
        if($request->hasFile("upfile")){
            //获取上传信息
            $file = $request->file("upfile");
            //确认上传的文件是否成功
            if($file->isValid()){
                //$picname = $file->getClientOriginalName(); //获取上传原文件名
                $ext = $file->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $filename = time().rand(1000,9999).".".$ext;
                $file->move("./upload/",$filename);
                                
                return response($filename); //输出
                exit();
            }
        }
    }


}
